﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using BookStore.Solution.BLL;
using BookStore.Solution.Entities;

namespace BookStore.Solution.Presentation
{
    public partial class ucBook : UserControl
    {
        public ucBook()
        {
            InitializeComponent();
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            btnSearch.Expanded = true;
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            FrmBook frm = new FrmBook();
            frm.ShowDialog();
            if (!frm.Visible)
            {
                LoadData();
            }
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            string productID="";
            if (grvListofBook.SelectedRows.Count > 0)
            {
                productID = grvListofBook.CurrentRow.Cells["ProductID"].Value.ToString();
                FrmBook frm = new FrmBook(productID,false);
                frm.ShowDialog();
                if (!frm.Visible)
                {
                    LoadData();
                }
            }
        }

        private void btnViewDetail_Click(object sender, EventArgs e)
        {
            string productID = "";
            if (grvListofBook.SelectedRows.Count > 0)
            {
                productID = grvListofBook.CurrentRow.Cells["ProductID"].Value.ToString();
                FrmBook frm = new FrmBook(productID, true);
                frm.ShowDialog();
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (grvListofBook.SelectedRows.Count > 0)
            {
                DialogResult dlr = MessageBox.Show("Bạn có chắc chắn xóa những sách đã chọn?", "Xác nhận xóa", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
                BookManager bookManager = new BookManager();
                string ProductID="";
                List<string> del = new List<string>();
                if (dlr == DialogResult.Yes)
                {
                    for (int i = 0; i < grvListofBook.SelectedRows.Count; i++)
                    {
                        ProductID = grvListofBook.SelectedRows[i].Cells["ProductID"].Value.ToString();
                        if (bookManager.Delete(ProductID) == 0)
                        {
                            del.Add(ProductID);
                        }
                    }
                    if (del.Count != 0)
                    {
                        String list = "";
                        for (int i = 0; i < del.Count; i++)
                            list += "," + del[i].ToString();
                        MessageBox.Show("Xóa thất bại các sách có ID: \n" + list, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    else
                    {
                        MessageBox.Show("Xóa thành công các sách đã chọn", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    LoadData();
                }
            }
            else
            {
                MessageBox.Show("Lựa chọn ít nhất 1 sách để xóa","Thông báo",MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void btnSearchbyID_Click(object sender, EventArgs e)
        {
            FrmSearchbyBookID frm = new FrmSearchbyBookID();
            frm.ShowDialog();
            if (!frm.Visible && frm.result != null)
            {
                LoadData_Search(frm.result);
            }
        }

        private void btnSearchbyName_Click(object sender, EventArgs e)
        {
            FrmSearchbyBookName frm = new FrmSearchbyBookName();
            frm.ShowDialog();
            if (!frm.Visible && frm.result != null)
            {
                LoadData_Search(frm.result);
            }
        }

        private void btnSearchbyAuthor_Click(object sender, EventArgs e)
        {
            FrmSearchbyBookAuthor frm = new FrmSearchbyBookAuthor();
            frm.ShowDialog();
            if (!frm.Visible && frm.result != null)
            {
                LoadData_Search(frm.result);
            }
        }

        private void btnSearchbyPublisher_Click(object sender, EventArgs e)
        {
            FrmSearchbyBookPublisher frm = new FrmSearchbyBookPublisher();
            frm.ShowDialog();
            if (!frm.Visible && frm.result != null)
            {
                LoadData_Search(frm.result);
            }
        }

        private void btnSeachbyCategory_Click(object sender, EventArgs e)
        {
            FrmSearchbyBookCategory frm = new FrmSearchbyBookCategory();
            frm.ShowDialog();
            if (!frm.Visible && frm.result != null)
            {
                LoadData_Search(frm.result);
            }
        }

        private void ucBook_Load(object sender, EventArgs e)
        {
            LoadData();
        }

        private void LoadData()
        {
            grvListofBook.Rows.Clear();
            BookManager bookManager = new BookManager();
            List<Product> list = bookManager.GetAll();
            foreach (var l in list)
            {
                List<Author> author = l.Authors.ToList<Author>();
                string lauthor = "";
                foreach (var a in author)
                {
                    lauthor += a.AuthorName.ToString()+",";
                }
                grvListofBook.Rows.Add(l.ProductID, l.ProductName, lauthor, l.CategoryID, l.PublisherID,
                    l.YearPublication, l.Version, l.PageNumber, l.Decription, l.Unit, l.UnitPrice, l.Quatity, l.Picture);
            }
        }

        private void LoadData_Search(List<Product> listResult)
        {
            grvListofBook.Rows.Clear();
            List<Product> list = listResult;
            foreach (var l in list)
            {
                List<Author> author = l.Authors.ToList<Author>();
                string lauthor = "";
                foreach (var a in author)
                {
                    lauthor += a.AuthorName.ToString() + ",";
                }
                grvListofBook.Rows.Add(l.ProductID, l.ProductName, lauthor, l.CategoryID, l.PublisherID,
                    l.YearPublication, l.Version, l.PageNumber, l.Decription, l.Unit, l.UnitPrice, l.Quatity, l.Picture);
            }
        }

        private void tsmiRefesh_Click(object sender, EventArgs e)
        {
            LoadData();
        }
    }
}
